#!/usr/bin/python3

import requests
import redis

weburl = 'http://127.0.0.1:5000'
# weburl = 'http://mqtt.nicelong.top:5001'

def _test_token_exist(key):
    data = {
        'token': key,
    }
    url = weburl + '/token'
    response = requests.post(url, json=data)

    # 解析响应数据
    return  response.json()

def test_token_exist():
    r = redis.Redis(host='localhost', port=6379, db=0)
    r.set("name1", "wml1")
    r.set("name2", "wml2")
    r.set("name3", "wml3")
    r.set("name4", "wml4")
    r.delete("name3", "name4")

    jresp = _test_token_exist("name1")
    assert jresp.get("message") == '-'
    assert jresp.get("val") == 1

    jresp = _test_token_exist("name2")
    assert jresp.get("message") == '-'
    assert jresp.get("val") == 1


    jresp = _test_token_exist("name3")
    assert jresp.get("message") == '-'
    assert jresp.get("val") == 0


    jresp = _test_token_exist("name4")
    assert jresp.get("message") == '-'
    assert jresp.get("val") == 0

    
def db_init():
    data = {
        'token': key,
    }
    url = weburl + '/db/init'
    print("ss")
    print("lsldfs")
    response = requests.get(url)

def db_new_token_1day():
    r = redis.Redis(host='localhost', port=6379, db=0)
    r.set("only-for-test", "1")
    data = {
        'token': "only-for-test",
        'day': 1,
        'sec': 60,
        'ower': 'test',
    }
    url = weburl + '/db/new_token'
    response = requests.post(url, json=data)
    print(response.json())

    # 解析响应数据
    return  response.json()

def db_new_token_1min():
    r = redis.Redis(host='localhost', port=6379, db=0)
    r.set("only-for-test", "1")
    data = {
        'token': "only-for-test",
        'day': 0,
        'sec': 120,
        'ower': 'test',
    }
    url = weburl + '/db/new_token'
    response = requests.post(url, json=data)
    print(response.json())

    # 解析响应数据
    return  response.json()


def db_new_token_by_ower():
    r = redis.Redis(host='localhost', port=6379, db=0)
    r.set("only-for-test", "1")
    
    ower = input("ower: ")
    day  = int(input("day: "))
    sec  = int(input("sec: "))

    data = {
        'token': "only-for-test",
        'day': day,
        'sec': sec,
        'ower': ower,
    }
    url = weburl + '/db/new_token'
    response = requests.post(url, json=data)
    print(response.json())

    # 解析响应数据
    return  response.json()

def fun_none():
    pass
OPTIONS = {
    "1": {
        "description": "token测试",
        "action": test_token_exist
    },
    "2": {
        "description": "db init",
        "action": db_init
    },
    "3": {
        "description": "db add 1天",
        "action": db_new_token_1day
    },
    "4": {
        "description": "db add 1分钟",
        "action": db_new_token_1min
    },
    "5": {
        "description": "db add 根据用户需求创建",
        "action": db_new_token_by_ower
    },
    "6": {
        "description": "todo4",
        "action": fun_none
    }
}

while True:
    for key, value in OPTIONS.items():
        print(f"{key}. {value['description']}")
    
    choice = input("Select an option (1-4): ")
    
    if choice in OPTIONS:
        OPTIONS[choice]["action"]()
    else:
        print("Invalid choice. Please enter a number between 1 and 4.")
